#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Create on May 2011

@author Wang Qiang
"""


import sys
import os
path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(path)

from QQWeiboAPI.qqAPI import QQAPI
from QQWeiboAPI import oauth
import unittest
import simplejson as json


class UserTest(unittest.TestCase):
    """
    Test for the user operation
    
    user/info
    user/update
    user/update_head
    user/other_info
    """

    consumer_key = ''
    consumer_secret = ''
    access_key = ''
    access_secret = ''
            
    def testConsumer(self):
        """test for loading consumer"""
        path = os.path.join(os.path.dirname(__file__), 'consumer.txt')
        f = open(path, 'r')
        consumerStr = f.read()
        f.close()
        consumer = json.loads(consumerStr)
        self.consumer_key = consumer['key']
        self.consumer_secret = consumer['secret']
        self.assertEqual(len(self.consumer_key), 32)
        self.assertEqual(len(self.consumer_secret), 32)
   
    def testAccessToken(self):
        """test for loading access_token"""
        path = os.path.join(os.path.dirname(__file__), 'access_token.txt')
        f = open(path, 'r')
        access_tokenStr = f.read()
        f.close()
        access_token = json.loads(access_tokenStr)
        self.access_key = access_token['key']
        self.access_secret = access_token['secret']
        self.assertEqual(len(self.access_key), 32)
        self.assertEqual(len(self.access_secret), 32)
    
    def testUserInfo(self):
        """test for user/info"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.info_user(format='json')
        self.assertEqual(res.ret, 0)
        print res.data.province_code
        print res.data.city_code
    
    def testUserUpdate(self):
        """test for user/update"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.info_user(format='json')
        myIntroduction = 'python 初学者'
        # all the paramaters are required
        res = testAPI.update_user(format='json', nick=res.data.nick, \
                               sex=res.data.sex, year=res.data.birth_year,\
                               month=res.data.birth_month, \
                               day=res.data.birth_day, countrycode=1,
                               provincecode=34, citycode=2, \
                               introduction=myIntroduction) 
        self.assertEqual(res.ret, 0)

    def testUserUpdateHead(self):
        """test for user/update_head"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        picFilename = os.path.join(os.path.dirname(__file__), 'soy.jpg')
        res = testAPI.update_head_user(format='json', pic=picFilename)
        self.assertEqual(res.ret, 0)

    def testUserOtherInfo(self):
        """test for user/other_info"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        picFilename = os.path.join(os.path.dirname(__file__), 'soy.jpg')
        res = testAPI.other_info_user(format='json', name='simple-is-better')
        self.assertEqual(res.ret, 0)
        print res.data.nick


if __name__ == '__main__':
    #unittest.main()
    fast = unittest.TestSuite()
    fast.addTest(UserTest('testUserOtherInfo'))
    unittest.TextTestRunner().run(fast)
